// ------------------------------------------------------------------------------------------------
// Created by:   Booner.Xu
// Create on:    Apr 05, 2014
//
//      (Apr 05, 2014 - Booner.Xu)
//      - Created the class file.
// ------------------------------------------------------------------------------------------------

package org.sprogram.utils
{
    import flash.utils.getDefinitionByName;
    import flash.utils.getQualifiedClassName;

    public class ClassUtil
    {
        /**
         * 根据对象获取类
         */
        public static function referenceClassByObj(obj:Object):Class
        {
            return referenceClassByString(getClassFullName(obj));
        }

        /**
         * 根据类名获取类
         */
        public static function referenceClassByString(str:String):Class
        {
            var ClassReference:Class;
            try
            {
                ClassReference = getDefinitionByName(str) as Class;
            }
            catch (error:Error)
            {

            }
            return ClassReference;
        }

        /**
         * 根据classname实例化Class
         * */
        public static function referenceClass(className:String):Object
        {
            var ClassReference:Class = referenceClassByString(className);
            var instance:Object;
            if (ClassReference != null)
            {
                try
                {
                    instance = new ClassReference();
                }
                catch (error:Error)
                {
                    trace("创建 " + className + " 失败")
                }
            }
            return instance;
        }

        /**
         * 获取class name全名
         * */
        public static function getClassFullName(value:Object):String
        {
            return getQualifiedClassName(value);
        }

        /**
         * 获取class name
         * */
        public static function getClassName(value:Object):String
        {
            var strCFN:String = getClassFullName(value);
            if (strCFN.indexOf("::") != -1)
            {
                return strCFN.substring(strCFN.indexOf("::") + 2);
            }
            else
            {
                return strCFN;
            }
        }
    }
}
